const app = getApp();
const {
  upTime,
  throttle
} = require('/util/public')
Page({
  data: {
    comsumeSore: '',
    shows: 'true',
    times: '',
    timess: '',
    oid: '',
    nums: '',
    synumshow: false,
    synum: '',
    cnxh: [],
    inforshow: '',
    numbes: '',
    coms: '',
    phones: '',
    orderlist: [],
    search: {
      idi: '',
      page: false
    },
    num: '0',
    order: [{
        title: "待支付",
        icon: '../../../image/otherImg/order_0.png',
        index: 1
      },
      {
        title: "待发货",
        icon: '../../../image/otherImg/order_1.png',
        index: 3
      },
      {
        title: "待收货",
        icon: '../../../image/otherImg/order_2.png',
        index: 4
      },
      {
        title: "还款中",
        icon: '../../../image/otherImg/order_3.png',
        index: 5
      },
      {
        title: "已完成",
        icon: '../../../image/otherImg/order_4.png',
        index: 6
      },
    ],
    order2: [{
        title: "客服电话",
        icon: '../../../image/other/newpic/vector.png',
        tab: 'server',
        index:1
      },
      {
        title: "买断/续租",
        icon: '../../../image/other/newpic/md.png',
        tab: 'navigateTo',
        url: "/pages/order/orderList/orderList",
        index: 5
      },

      // { title: "我要续租", icon: '../../../image/otherImg/xz.png', tab: 'navigateTo', url: "/pages/order/orderList/orderList", index: 5 },
      {
        title: "修改信息",
        icon: '../../../image/other/newpic/xgx.png',
        tab: 'navigateTo',
        url: "/pages/user/authentication/authentication",
        item:3
      },
      {
        title: "投诉建议",
        icon: '../../../image/other/newpic/icon_06.png',
        tab: 'navigateTo',
        url: "/pages/user/complaint/complaint",
        index: 0
      },


      // { title: "分享赚钱", icon: '../../../image/other/newpic/fx.png', tab: 'navigateTo', url: "/pages/fenxiao/myInfo/myInfo" },
      // { title: "授权营销", icon: 'https://static.jxqyfw.com/upload/20220727/2542e578913b40fda4414bfc6d35d377.png', tab: 'navigateTo', url: "/pages/fenxiao/sqyx/sqyx" },
      {
        title: "优惠券",
        icon: '../../../image/other/newpic/yjq.png',
        tab: 'navigateTo',
        url: "/pages/order/coupons/coupons",
        item:4
      },
    ],
    server: [{
        title: "商家入驻",
        icon: 'icon-shangpu',
        tab: 'navigateTo',
        url: "/pages/user/enter/enter"
      },
      {
        title: "身份认证",
        icon: 'icon-shenfenzheng',
        tab: 'navigateTo',
        url: "/pages/user/authentication/authentication"
      },
      {
        title: "收货地址",
        icon: 'icon-dizhi',
        tab: 'address'
      },
      {
        title: "在线客服",
        icon: 'icon-zaixiankefu',
        tab: 'server'
      },
      {
        title: "退出登录",
        icon: 'icon-tuichudenglu',
        tab: 'logout'
      },
    ],
    banner: 'https://static.yirenzuji.com/upload/20211112/de2815fa0c7148028f6790e8700204f1.png',
    loginShow: true,
    login: false,

    prudects: []

  },
  navigateTos(e) {


    my.switchTab({
      url: e.currentTarget.dataset.url
    });

  },
  // numbes: res.data.data[0].productOrderShipment.number,
  // coms: res.data.list[0].productOrderShipment.company,
  // phones
  getLogistics(id, data) {
    let datas = {};
    datas.com = this.data.coms;
    datas.num = this.data.numbes;
    datas.phone = this.data.phones;
    datas.oid = this.data.oid;
    app.order.getKuaiDi(datas)
      .then(res => {
        console.log("=======================>>", res)
        if (res.code == 200) {
          this.setData({
            inforshow: res.data.data[0].context
          })

        } else {
          this.setData({
            inforshow: ''
          })
        }


      })
  },
  getinfo() {
    let that = this;
    let e = {};
    e.uid = my.getStorageSync({
      key: 'user'
    }).data.id;
    app.order.getinfo(e)
      .then(res => {
        console.log("查询信息111", res)

        if (res.data == null) {
          that.setData({
            shows: true
          })
          console.log("没开")
        } else {
          if (res.data.status != '0') {
            that.setData({
              shows: false,
              comsumeSore: res.data.comsumeSore,
              timess: /\d{4}-\d{1,2}-\d{1,2}/g.exec(res.data.endTime)
            })
          } else {
            that.setData({
              shows: true
            })
          }

          console.log("开了")
        }
      })
  },
  clearstor() {
    // .js

    my.clearStorageSync()
    my.showToast({
      type: 'exception ',
      content: '清理缓冲成功！',
      duration: 1000,
    });
    this.setData({
      login: false,
      synumshow: false
    })

  },
  gopage() {

    let userInfo = my.getStorageSync({
      key: 'user'
    })

    if (!userInfo.data) {
      my.showToast({
        type: 'exception ',
        content: '你还没有登陆',
        duration: 1000,
      });

      return
    }
    my.navigateTo({
      url: '/pages/order/ordervip/ordervip'
    });
  },
  onHide() {

    my.hideLoading();
  },
  onShow() {

    //my.hideTabBar()

    let user = my.getStorageSync({
      key: 'user'
    });

    this.getHome()
    if (user.data) {
      this.getUser();
      this.get_meg();
      this.mesfive();


      this.get_megs();
      // this.getinfo();
      // this.pageorder();
    } else {
      this.setData({
        login: false
      })
      console.log("清掉了")
    }
    var myDate = new Date().getTime(); //获取系统当前时间
    console.log("!!!!", myDate)
    var times = upTime("%Y-%M-%D", myDate)
    this.setData({
      times: times
    })

    console.log("!!!!", times)
    // this.config();

  },
  pageorder() {
    app.order.pageorder()
      .then(res => {
        console.log("查询", res)
        res.data.data.forEach(item => {
          item.expiretime = item.expiretime ? upTime("%Y-%M-%D", item.expiretime) : '';

          // if (item.payment == 1) {
          //   puyAmount += item.price / 100
          // }
        })

        let item = res.data
        console.log("查询111", item)
        this.setData({
          orderlist: item.data
        })

      }).catch(v => {
        console.log("1111223344+++", v);

      });

  },
  get_megs() {

    let data = {
      status: 4,
      pageNo: 1,
      pageSize: 10,
    }
    app.order.page(data)
      .then(res => {

        if (res.data.totalCount > 0 && res.data.data[0].productOrderShipment != null) {
          this.setData({
            numbes: res.data.data[0].productOrderShipment.number,
            coms: res.data.data[0].productOrderShipment.company,
            phones: res.data.data[0].phone,
            oid: res.data.data[0].id
          })
          this.getLogistics()
        }


      }).catch(v => {
        console.log("1111223344+", v);
      });
  },
  mesfive() {

    let data = {
      status: 5,
      pageNo: 1,
      pageSize: 10,
    }
    app.order.page(data)
      .then(res => {
        if (res == undefined) {
          this.setData({
            login: false,
            loginShow: true
          })

        } else {

          this.setData({
            synumshow: true,
            synum: 4 - res.data.totalCount
          })
          console.log("定好11223", this.data.synum)

        }
      }).catch(v => {
        console.log("1111223344++", v);
      });
  },
  // 获取数据
  get_meg() {

    let data = {
      status: 1,
      pageNo: 1,
      pageSize: 10,
    }
    app.order.page(data)
      .then(res => {
        if (res == undefined) {
          this.setData({
            login: false,
            loginShow: true
          })

        } else {


          console.log("定好", res)
          this.setData({
            num: res.data.totalCount
          })
        }
      }).catch(v => {
        console.log("1111223344++", v);
      });
  },
  onLoad() {

    app._init_(this)
    // app.editTabbar();


  },

  config() {

    var that = this;
    app.other.config()
      .then(res => {
        console.info("响应码:", res.data);
        for (const key in res.data.data) {
          // console.info("响应码:", key);
          if (res.data.data.hasOwnProperty(key)) {
            const element = res.data.data[key];
            if (element.name == "shops_enter") {
              that.setData({
                idi: element.content
              })
            }
          }
        }



      })




  },


  getHome() {

    app.other.home().then(res => {

      this.setData({
        cnxh: JSON.parse(res.data.eventsGoods).prudects
      })


      // this.dataPromise(love).then(json => {
      //   console.log("++++++++++++", json)
      //   res.data.wntj.plist = json
      //   this.setData({
      //     cnxh: res.data.wntj,
      //   })
      // })

    })
  },
  // 转价格
  dataPromise(list) {
    return new Promise((relose, reject) => {
      list.forEach((item) => {
        this.getminPrice(item.params)
          .then(res => {
            item.price = res
          })
      })

      relose(list)
    })
  },

  getminPrice(params) {
    return new Promise((resole, reject) => {
      let prices = []
      for (let i = 0; i < params.length; i++) {
        for (let t = 0; t < params[i].term_list.length; t++) {
          for (let s = 0; s < params[i].term_list[t].specifications.length; s++) {
            let price = (params[i].term_list[t].specifications[s].money / params[i].term_list[t].value / 100).toFixed(2)
            prices.push(price)
          }
        }
      }
      let minprice = Math.min.apply(null, prices);
      resole(minprice)
    })
  },
  // login
  getUser() {
    let user = my.getStorageSync({
      key: 'user'
    });

    // 判断有没有登录
    if (user.data) {
      console.log("user", user)
      var tel = user.data.phone;
      tel = "" + tel;
      var tel1 = tel.replace(tel.substring(3, 7), "****")
      //console.log("5555", tel1);
      this.setData({
        user_header: user.data.header,
        user_name: user.data.uname,
        ipone: tel1,
        loginShow: false,
        login: true
      })
    } else {
      this.setData({
        user_header: '',
        user_name: '',
        ipone: '',
        loginShow: true,
        login: false
      })
    }
  },
  login() {

    this.setData({
      loginShow: true
    })
  },

  onLgin(data) {
    console.log("6666", data)
    if (data.login) {
      setTimeout(() => {
        this.get_meg();
        this.mesfive();
      }, 2000)

    }


    this.setData(data)

  },

  logout() {
    this.setData({
      user_header: '',
      user_name: '',
      ipone: '',
      // loginShow: true,
      login: false
    })
    my.clearStorage();

    my.showToast({
      type: 'success',
      content: '退出成功',
      duration: 1000,
    });
  },

  // 我的服务
  // 跳转
  navigateTo: throttle(function (e) {

    console.log("6666", e)
    if (e.currentTarget.dataset.url == '/pages/product/detail/detail') {
      my.navigateTo({

        url: e.currentTarget.dataset.url + `?param=${encodeURIComponent(JSON.stringify(e.currentTarget.dataset.param))}`,

      })
    } else {


      let userInfo = my.getStorageSync({
        key: 'user'
      })

      if (!userInfo.data) {
        my.showToast({
          type: 'exception ',
          content: '你还没有登陆',
          duration: 1000,
        });
        my.hideLoading();
        this.setData({
          loginShow: true
        })
        return
      }
      //  登录后  
      my.navigateTo({

        url: e.currentTarget.dataset.url + `?param=${encodeURIComponent(JSON.stringify(e.currentTarget.dataset.param))}`,

      })
    }
    // app.navigateTo(e)
  }, 300),
  // 地址
  address() {
    my.getAddress({
      success: (res) => {
        // this.setData({
        //     tit: res.result.fullname,   // 姓名
        //     province: res.result.prov,   // 省
        //     city: res.result.city,        //市
        //     district: res.result.area,   // 区
        //     street: res.result.street,   //路
        //     address: res.result.address, //地址
        //     phone: res.result.mobilePhone, //电话
        // });
      },
      fail: () => {

      }
    });
  },
  // 在线客服-电话
  server() {
    my.makePhoneCall({
      number: '4008856357'
    });
  }
});